/*
 * SFX - Sound eFfectX (with on/off support)
 *
 * Copyright 2007-8 James W. Hofmann
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

import flash.media.Sound;
import flash.display.MovieClip;
import flash.media.SoundChannel;
import flash.media.SoundTransform;

class SFX
{

	private static var s_placement = new SPlacement();
	public static var placement = false;
	private static var s_gameover = new SGameover();
	public static var gameover = false;
	private static var s_fortdeath = new SFortDeath();
	public static var fortdeath = false;

	private static var s_creepdeath1 = new SCreepDeath1();
	private static var s_creepdeath2 = new SCreepDeath2();
	private static var s_creepdeath3 = new SCreepDeath3();
	public static var creepdeath = false;

	private static var s_interfacehi = new SInterfaceHi();
	public static var interfacehi = false;
	private static var s_interfacelo = new SInterfaceLo();
	public static var interfacelo = false;
	private static var s_nextwave = new SNextWave();
	public static var nextwave = false;

	private static var s_music = new SMusic();
	public static var musicplaying = false;

	public static function up(?z : Dynamic)
	{
	
		if (FlashClient.sndvol>0)
		{
			try
			{
				var trans = new SoundTransform(FlashClient.sndvol,0);
				if (placement)
					s_placement.play(0,0,trans);
				if (gameover)
					s_gameover.play(0,0,trans);
				if (fortdeath)
					s_fortdeath.play(0,0,trans);
				if (interfacehi)
					s_interfacehi.play(0,0,trans);
				if (interfacelo)
					s_interfacelo.play(0,0,trans);
				if (nextwave)
					s_nextwave.play(0,0,trans);
				if (creepdeath)
				{
					var nextdeath = Math.random();
					if (nextdeath<0.33)
						s_creepdeath1.play(0,0,trans);	
					else if (nextdeath<0.66)
						s_creepdeath2.play(0,0,trans);	
					else
						s_creepdeath3.play(0,0,trans);	
				}
			}
			catch (foo : Dynamic) {}

		if (musicplaying)
		{ // try to stop music?
			if (!(FlashClient.musicon && FlashClient.mainmenu.visible))
			{
				flash.media.SoundMixer.stopAll(); // this is overkill
				 // but something seems to be up in the haxe flash
				 // soundchannel api....it wouldn't work
				 				 
				musicplaying = false;								
			}
		}
		else // try to start music?
		{
			if (FlashClient.musicon && FlashClient.mainmenu.visible)
			{
				s_music.play(0,9999); // this is stupid but flash
				 // doesn't allow infinite-loop?
				musicplaying = true;				
			}			
		}

		clearsounds();
		
		}
	}
	
	public static inline function clearsounds()
	{
		placement = false;
		gameover = false;
		fortdeath = false;
		creepdeath = false;
		interfacehi = false;
		interfacelo = false;
		nextwave = false;		
	}
	
}

class SPlacement extends Sound {}
class SGameover extends Sound {}
class SFortDeath extends Sound {}
class SCreepDeath1 extends Sound {}
class SCreepDeath2 extends Sound {}
class SCreepDeath3 extends Sound {}
class SInterfaceHi extends Sound {}
class SInterfaceLo extends Sound {}
class SNextWave extends Sound {}

class SMusic extends Sound {}
